﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Charting;
using Baosight.ZNH.ZMCA.UI.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Microsoft.Practices.Unity;
using Baosight.ZNH.ZMCA.UI.Model;

namespace Baosight.ZNH.ZMCA.UI
{
    /*
     * 工作指示添加页面
     */
    public partial class ZMCA45 : EFViewCmes
    {
        public event EventHandler SaveSuccess;
        public EIBlock blockUser = null;
        public EIBlock blockClass = null;
        public ZMCA45()
        {
            InitializeComponent();
        }

        public void ZMCA45_Loaded(object sender, RoutedEventArgs e)
        {
            this.SaveButton.Visibility = Visibility.Visible;
            this.fdStatus.IsEnabled = true;
            this.fdFeedbackPersonId.IsEnabled = true;
            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCA42");
            eiInfo.Set("method", "initDataZMCA45");

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, onLoadCompleted);
        }

        private void onLoadCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.GetStatus() == -1)
            {
                EFWindow.Alert("数据加载失败！");
                return;
            }
            else
            {
                e.EIInfo.AddBlock(getProblemType());
                this.UpdateData(e.EIInfo);
                return;
            }
        }

        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
            if (initData.Attributes.ContainsKey("fdKeyId"))
            {
                this.fdKeyId.Value = Convert.ToString(initData.Attributes["fdKeyId"]);
            }
        }

        public void Save(object sender, RoutedEventArgs e)
        {
            if (IsLegal())
            {
                EIInfo eiInfo = new EIInfo();
                eiInfo.Set("service", "ZMCA42");
                eiInfo.Set("method", "saveZMCA45");

                EIBlock block = new EIBlock("saveBlock");
                EIRow row = new EIRow();
               
                AddColumnToBlock(block, "fdProblemId", row, fdKeyId.Value);
                AddColumnToBlock(block, "fdProblemTypeId", row, fdProblemTypeId.Value);
                AddColumnToBlock(block, "fdCheckpersonId", row, fdCheckPersonId.Value);
                AddColumnToBlock(block, "fdCheckTime", row, fdCheckTime.Value);
                AddColumnToBlock(block, "fdCheckContent", row, fdCheckContent.Value);
                AddColumnToBlock(block, "fdFeedbackPersonId", row, fdFeedbackPersonId.Value);
                AddColumnToBlock(block, "fdStatus", row, fdStatus.Value);
                block.AddRow(row);
                eiInfo.AddBlock(block);
                this.Submit(eiInfo, onSaveCompleted);
            }
        }

        private void onSaveCompleted(object sender, DoWorkCompletedEventArgs e)
        {

            if (e.EIInfo.GetStatus() == -1)
            {
                EFWindow.Alert("数据保存失败!");
                return;
            }
            else
            {
                if (SaveSuccess != null)
                {
                    SaveSuccess(sender, e);
                }
                if (this.Parent is EFWindow)
                {
                    (this.Parent as EFWindow).Close();
                }
                EFWindow.Alert("数据保存成功!");
                return;
            }

        }



        private void AddColumnToBlock(EIBlock block, string colName, EIRow row, object colValue)
        {
            EIColumn column = new EIColumn();
            column.SetName(colName);
            row.Add(colName, colValue);
            block.AddColumn(column);
        }

        public bool IsLegal()
        {
            if (!this.infoGrid.ValidateField())
            {
                EFWindow.Alert("校验没有通过，请查看错误重新输入");
                return false;
            }
            return true;
        }
        /// <summary>
        /// 构造问题类型的block
        /// </summary>
        /// <returns></returns>
        public EIBlock getProblemType()
        {
            EIBlock block = new EIBlock("ProblemType");

            EIColumn column = new EIColumn();
            column.Name = "fdProblemType";
            column.SetTypeLabel("C");
            block.AddColumn(column);

            column = new EIColumn();
            column.Name = "fdProblemTypeId";
            column.SetTypeLabel("C");
            block.AddColumn(column);

            List<EIRow> _list = new List<EIRow>();
            EIRow row = new EIRow();
            row.Add("fdProblemType", "生产问题");
            row.Add("fdProblemTypeId", "1");
            _list.Add(row);

            row = new EIRow();
            row.Add("fdProblemType", "安全隐患");
            row.Add("fdProblemTypeId", "2");
            _list.Add(row);

            row = new EIRow();
            row.Add("fdProblemType", "安全三违");
            row.Add("fdProblemTypeId", "3");
            _list.Add(row);

            block.AddRows(_list);
            return block;
        }

        public void SetInstructionValue(Instruction newInstruction)
        {
        
            EIInfo eiInfo = new EIInfo();
            eiInfo.AddBlock(blockClass);
            eiInfo.AddBlock(blockUser);
            this.UpdateData(eiInfo);
            this.fdCheckContent.Value = newInstruction.FdCheckContent;
            this.fdCheckPersonId.Value = newInstruction.FdCheckpersonId;
            this.fdFeedbackPersonId.Value = newInstruction.FdFeedbackPersonId;
            this.fdCheckTime.Value = newInstruction.FdCheckTime;
            this.fdProblemTypeId.Value = newInstruction.FdProblemTypeId;
            this.fdStatus.Value = newInstruction.FdStatus;
            this.fdFeedbackTime.Value = newInstruction.FdFeedbackTime;
            this.fdFeedbackContent.Value = newInstruction.FdFeedbackContent;
            this.SaveButton.Visibility = Visibility.Collapsed;
            this.fdStatus.IsEnabled = false;
            this.fdFeedbackPersonId.IsEnabled = false;
            this.fdFeedbackContent.Visibility = Visibility.Visible;
            this.fdFeedbackTime.Visibility = Visibility.Visible;
        }

        //设置新增时的页面布局
        public void SetNew()
        {
            this.fdFeedbackContent.Visibility = Visibility.Collapsed;
            this.fdFeedbackTime.Visibility = Visibility.Collapsed;
            this.fdStatus.Value = "新增";
        }
    }
}
